<?php

if (!isset($_POST['name'])
    || !isset($_POST['description'])
    || !isset($_POST['profile'])
    || !isset($_POST['latitude'])
    || !isset($_POST['longitude'])
    || !isset($_POST['companyUrl'])
) {
    echo("Missing parameters from add");
    exit();
}
$name = $_POST['name'];
$description = $_POST['description'];
$profile = $_POST['profile'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
$companyUrl = $_POST['companyUrl'];

// import database connection variables and connectToMysql method
require_once __DIR__ . '/db_config.php';

$mysqli = connectToMysql();

$query = "INSERT INTO location ( latitude, longitude) VALUES ( '$latitude', '$longitude')";

if ($result = $mysqli->query($query)) {
    /* fetch object array */
    $locationId = $mysqli->insert_id;

    $query = "INSERT INTO Company ( name, description, profile, location, companyUrl) VALUES ( '$name', '$description', '$profile', '$locationId', '$companyUrl')";

    if ($result = $mysqli->query($query)) {
        /* fetch object array */
        echo json_encode($mysqli->insert_id, JSON_NUMERIC_CHECK);
    } else {
        print mysqli_error($mysqli);
    }

} else {
    print mysqli_error($mysqli);
}


/* close connection */
$mysqli->close();
?>